<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>QUnit for ASN1 Encoder Class 'asn1.js'</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="qunit.js"></script>
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />

<!-- getPEM -->

<script src="../ext/jsbn.js"></script>
<script src="../ext/jsbn2.js"></script>
<script src="../ext/base64.js"></script>
<script src="../src/base64x-1.1.js"></script>
<script src="../src/asn1hex-1.1.js"></script>
<script src="../src/asn1-1.0.js"></script>
<script src="../src/asn1x509-1.0.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){

// ======= TEST ===============================================================
// z2 self
  var sCer1PEM = "" +
"-----BEGIN CERTIFICATE-----\r\n" + 
"MIIBqDCCAVKgAwIBAgIJAJ53Xn2fQ6vJMA0GCSqGSIb3DQEBBQUAMBoxCzAJBgNV\r\n" + 
"BAYTAkpQMQswCQYDVQQKEwJ6MjAeFw0xMDA1MzEwNjE3NTZaFw0yMDA1MjgwNjE3\r\n" + 
"NTZaMBoxCzAJBgNVBAYTAkpQMQswCQYDVQQKEwJ6MjBcMA0GCSqGSIb3DQEBAQUA\r\n" + 
"A0sAMEgCQQC5sAMpUF/i7WDsVondzfW5TQWrQW4WQ7HKP5b8Ry7rH2LtR2iX8Vmu\r\n" + 
"mLfzrezseXHetfZNmiWQHE3HpJdy5aArAgMBAAGjezB5MB0GA1UdDgQWBBS7MIUo\r\n" + 
"gYmDIf2DOqOcwYCKCb3GjDBKBgNVHSMEQzBBgBS7MIUogYmDIf2DOqOcwYCKCb3G\r\n" + 
"jKEepBwwGjELMAkGA1UEBhMCSlAxCzAJBgNVBAoTAnoyggkAnndefZ9Dq8kwDAYD\r\n" + 
"VR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAANBAFhZ88q7ZS7THEcPJ8ogQ+78nyAn\r\n" + 
"DN7F1FsLp7jE9oM3kYQl6KYy0pwch6Wd6ypW86pel4JFOEr+LnVGODaoxFE=\r\n" + 
    "-----END CERTIFICATE-----\r\n";

  var hCer1 = "308201a830820152a0030201020209009e775e7d9f43abc9300d06092a864886f70d0101050500301a310b3009060355040613024a50310b3009060355040a13027a32301e170d3130303533313036313735365a170d3230303532383036313735365a301a310b3009060355040613024a50310b3009060355040a13027a32305c300d06092a864886f70d0101010500034b003048024100b9b00329505fe2ed60ec5689ddcdf5b94d05ab416e1643b1ca3f96fc472eeb1f62ed476897f159ae98b7f3adecec7971deb5f64d9a25901c4dc7a49772e5a02b0203010001a37b3079301d0603551d0e04160414bb30852881898321fd833aa39cc1808a09bdc68c304a0603551d23044330418014bb30852881898321fd833aa39cc1808a09bdc68ca11ea41c301a310b3009060355040613024a50310b3009060355040a13027a328209009e775e7d9f43abc9300c0603551d13040530030101ff300d06092a864886f70d01010505000341005859f3cabb652ed31c470f27ca2043eefc9f20270cdec5d45b0ba7b8c4f68337918425e8a632d29c1c87a59deb2a56f3aa5e978245384afe2e75463836a8c451";

test("ASN1Object test", function() {
equal((new KJUR.asn1.ASN1Object({tlv: "030101"})).getEncodedHex(), "030101", "{tlv: 030101}");
});

test("Boolean Test", function() {
  equal(new KJUR.asn1.DERBoolean(true).getEncodedHex(), "0101ff", "Boolean TRUE");
  equal(new KJUR.asn1.DERBoolean(false).getEncodedHex(), "010100", "Boolean TRUE");
});

test("Integer Test", function() {
  var d = new KJUR.asn1.DERInteger();
  var _DERInteger = KJUR.asn1.DERInteger;
  d.setByBigInteger(new BigInteger("3", 16));
  equal(d.getEncodedHex(), "020103", "setByBigInteger(BI('3',16))");
  d.setByBigInteger(new BigInteger("3f", 16));
  equal(d.getEncodedHex(), "02013f", "setByBigInteger(BI('3f',16))");
  d.setByBigInteger(new BigInteger("254", 10));
  equal(d.getEncodedHex(), "020200fe", "setByBigInteger(BI('254',10))");
  d.setByBigInteger(new BigInteger("-3", 16));
  equal(d.getEncodedHex(), "0201fd", "setByBigInteger(BI('-3',16))");
  d.setByBigInteger(new BigInteger("-1", 16));
  equal(d.getEncodedHex(), "0201ff", "setByBigInteger(BI('-1',16))");
  d.setByInteger(-3);
  equal(d.getEncodedHex(), "0201fd", "setByInteger(-3)");

  equal(new _DERInteger({'bigint': new BigInteger("-3", 16)}).tohex(), "0201fd", "new DERInteger({bigint: new BigInteger(-3, 16)})");
  equal(new _DERInteger({"int": -3}).tohex(), "0201fd", "new DERInteger({int: -3})");
  equal(new _DERInteger({hex: "fd"}).tohex(), "0201fd", "new DERInteger({hex: fd})");
  equal(new _DERInteger({hex: "00fd"}).tohex(), "020200fd", "new DERInteger({hex: 00fd})");
  equal(new _DERInteger(-3).tohex(), "0201fd", "new DERInteger(-3)");
});

test("DERBitString.setByBinaryString test", function() {
var d = new KJUR.asn1.DERBitString();

d.setByBinaryString("1");
equal(d.tohex(), "03020780", "1 unused=7");
d.setByBinaryString("001");
equal(d.tohex(), "03020520", "001 unused=5");
d.setByBinaryString('1011');
equal(d.tohex(), "030204b0", "1011 > ub=4, b0"); 
d.setByBinaryString('01011');
equal(d.tohex(), "03020358", "01011 > ub=3, b0"); 
d.setByBinaryString('11111111');
equal(d.tohex(), "030200ff", "11111111 > ub=0, ff"); 
d.setByBinaryString('111111111');
equal(d.tohex(), "030307ff80", "111111111 > ub=7, ff80"); 
});

test("BitString Test", function() {
var d = new KJUR.asn1.DERBitString();
d.setHexValueIncludingUnusedBits("04bac0");
equal(d.tohex(), "030304bac0", "setHexValueIncludingUnusedBits > ub=4, bac0");
d.setUnusedBitsAndHexValue(5, "bad0");
equal(d.tohex(), "030305bad0", "setUnusedBitsAndHexValue > ub=5, bad0");

d.setByBooleanArray([true, false, false, true, true]);
equal(d.tohex(), "03020398", "setByBooleanArray([t,f,f,t,t]) 10011 > ub=3, b0"); 

d = new KJUR.asn1.DERBitString({"hex":"04bac0"});
equal(d.tohex(), "030304bac0", "constructor hex 04bac0");

d = new KJUR.asn1.DERBitString({"bin":"1011"});
equal(d.tohex(), "030204b0", "constructor bin 1011"); 
d = new KJUR.asn1.DERBitString({"bin":"0000011"});
equal(d.tohex(), "03020106", "constructor bin 0000011"); 


var d4 = new KJUR.asn1.DERBitString({"array": [true,false,true,true]});
equal(d4.tohex(), "030204b0", "constructor array [t,f,t,t]"); 
var d5 = new KJUR.asn1.DERBitString({obj:{seq: [{int: 3},{prnstr: {str: "aaa"}}]}});
equal(d5.tohex(), "030b0030080201031303616161", "constructor params asn1");
});

test("OctetString Test", function() {
  var d = new KJUR.asn1.DEROctetString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "0403616161", "OctetString aaa");
  d.setStringHex('000102');
  equal(d.getEncodedHex(), "0403000102", "OctetString x000102");
  var d2 = new KJUR.asn1.DEROctetString({hex:'010203'});
  equal(d2.getEncodedHex(), "0403010203", "constructor params hex");
  var d3 = new KJUR.asn1.DEROctetString({str:'aaa'});
  equal(d3.getEncodedHex(), "0403616161", "constructor params str aaa");
  var d4 = new KJUR.asn1.DEROctetString({obj:{seq: [{int: 3},{prnstr: {str: "aaa"}}]}});
  equal(d4.getEncodedHex(), "040a30080201031303616161", "constructor params asn1");
});

test("Null Test", function() {
  var d = new KJUR.asn1.DERNull();
  expect(1);
  equal(d.getEncodedHex(), "0500", "Null");
});

test("ObjectIdentifier Test", function() {
  var d = new KJUR.asn1.DERObjectIdentifier();

  expect(5);
  d.setValueHex('550406');
  equal(d.getEncodedHex(), "0603550406", "setValueHex('550406') C:2.5.4.6");
  d.setValueOidString('2.5.4.6');
  equal(d.getEncodedHex(), "0603550406", "setValueOidString('2.5.4.6') C:2.5.4.6");
  d.setValueOidString('1.2.34.56789');
  equal(d.getEncodedHex(), "06052a2283bb55", "setValueOidString('1.2.34.56789')");
  var d4 = new KJUR.asn1.DERObjectIdentifier({'oid':'2.5.4.6'});
  equal(d4.getEncodedHex(), "0603550406", "constructor oid 2.5.4.6");

  var d5 = new KJUR.asn1.DERObjectIdentifier({'oid':'0.1.1.2.2'});
  equal(d5.getEncodedHex(), "060401010202", "oid 0.1.1.2.2");
});

test("UTF8String Test", function() {
  var d = new KJUR.asn1.DERUTF8String();
  d.setString('aaa');
  equal(d.getEncodedHex(), "0c03616161", "UTF8String aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "0c03626262", "UTF8String bbb");
  d.setString('あ');
  equal(d.getEncodedHex(), "0c03e38182", "UTF8String あ");
});

test("NumericString Test", function() {
  var d = new KJUR.asn1.DERNumericString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1203616161", "NumericString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1203626262", "NumericString bbb");
});

test("PrintableString Test", function() {
  var d = new KJUR.asn1.DERPrintableString();
  expect(2);
  d.setString('aaa');
  equal(d.getEncodedHex(), "1303616161", "PrintableString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1303626262", "PrintableString bbb");
});

test("TeletexString Test", function() {
  var d = new KJUR.asn1.DERTeletexString();
  expect(2);
  d.setString('aaa');
  equal(d.getEncodedHex(), "1403616161", "TeletexString aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1403626262", "TeletexString bbb");
});

test("IA5String Test", function() {
  var d = new KJUR.asn1.DERIA5String();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1603616161", "IA5String aaa");
  d.setString('bbb');
  equal(d.getEncodedHex(), "1603626262", "IA5String bbb");
  d.setString('あ');
  equal(d.getEncodedHex(), "1603e38182", "IA5String あ");
});

test("VisibleString Test", function() {
  var d = new KJUR.asn1.DERVisibleString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1a03616161", "VisibleString aaa=1a03616161");
  var d2 = new KJUR.asn1.DERVisibleString({str: 'bbb'});
  equal(d2.getEncodedHex(), "1a03626262", "VisibleString bbb=1a03626262");
});

test("BMPString Test", function() {
  var d = new KJUR.asn1.DERBMPString();
  d.setString('aaa');
  equal(d.getEncodedHex(), "1e03616161", "BMPString aaa=1a03616161");
  var d2 = new KJUR.asn1.DERBMPString({str: 'bbb'});
  equal(d2.getEncodedHex(), "1e03626262", "BMPString bbb=1a03626262");
});

test("UTCTime Test", function() {
var _DERUTCTime = KJUR.asn1.DERUTCTime;
var d;

d = new _DERUTCTime();
d.setString('130430125959Z');
equal(d.tohex(),
      "170d3133303433303132353935395a",
      "d.setString(130430125959Z)");

d = new _DERUTCTime();
d.setByDate(new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0)));
equal(d.getEncodedHex(),
      "170d3135303133313030303030305a",
      "d.setByDate(new Date(?2015/01/31 00:00:00?))");

d = new _DERUTCTime();
d.setByDateValue(2015, 1, 31, 0, 0, 0);
equal(d.getEncodedHex(),
      "170d3135303133313030303030305a", 
      "d.setByDateValue(2015,1,31,0,0,0)");

// -- implicit constructor test --
d = new _DERUTCTime({'date':new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))});
equal(d.tohex(),
      "170d3135303133313030303030305a",
      "new DERUTCTime({date: new Date(?2015/01/31 00:00:00?)})");

d = new _DERUTCTime("150131000000Z");
equal(d.tohex(),
      "170d3135303133313030303030305a",
      "new DERUTCTime(20150131000000Z)");

d = new _DERUTCTime("150131000000.123Z");
equal(d.tohex(),
      "17113135303133313030303030302e3132335a",
      "new DERUTCTime(20150131000000.123Z)");

// -- JSON constructor test --
d = new _DERUTCTime({
    'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))
});
equal(d.tohex(),
      "170d3135303133313030303030305a",
      "new DERUTCTime({date: new Date(?2015/01/31 00:00:00?)})");

d = new _DERUTCTime({
    'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 120)),
    millis: true
});
equal(d.tohex(),
      "17103135303133313030303030302e31325a",
      "new UTCTime({date: new Date(?2015/01/31 00:00:00.12?)})");

d = new _DERUTCTime({str: "150131000000Z"});
equal(d.tohex(),
      "170d3135303133313030303030305a",
      "new DERUTCTime({str: 150131000000Z})");

d = new _DERUTCTime({str: "150131000000.12Z"});
equal(d.tohex(),
      "17103135303133313030303030302e31325a",
      "new DERUTCTime({str: 150131000000.123Z})");

d = new _DERUTCTime();
var d5hex = d.tohex()
equal(d5hex.substr(0, 4), "170d", "new DERUTCTime() -> 170d...");

// now + millis test
// - fraction of second may not include with one in a hundred chance
// - so any of three times is fine, it will be fine.
var d6flag = false;
var s1 = "";
for (var i = 0; i < 3; i++) {
    var startMsec = new Date();
    while (new Date() - startMsec < 258);
    d = new _DERUTCTime({millis: true});
    var d6hex = d.tohex();
    if (d6hex.substr(0, 2) != "17") continue;
    var hS = ASN1HEX.getV(d6hex, 0);
    var s = hextoutf8(hS);
    s1 += s + "/";
    if (s.indexOf(".") == -1) continue;
    d6flag = true;
}
equal(d6flag, true, "new DERUTCTime({millis: true}): " + s1);

});

test("GeneralizedTime Test", function() {
var _DERGeneralizedTime = KJUR.asn1.DERGeneralizedTime;    
var d;

// setter method test
d = new _DERGeneralizedTime();
d.setString('20130430125959Z');
equal(d.getEncodedHex(),
      "180f32303133303433303132353935395a", 
      "d.setString(20130430125959Z)");

d = new _DERGeneralizedTime();
d.setByDate(new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0)));
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "d.setByDate(new Date(?2015/01/31 00:00:00?))");

d = new _DERGeneralizedTime();
d.setByDateValue(2015, 1, 31, 0, 0, 0);
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "d.setByDateValue(2015,1,31,0,0,0)");

// -- implicit constructor test --
d = new _DERGeneralizedTime(new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0)));
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "new GeneralizedTime(new Date(?2015/01/31 00:00:00?))");

d = new _DERGeneralizedTime("20150131000000Z");
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "new GeneralizedTime(20150131000000Z)");

d = new _DERGeneralizedTime("20150131000000.123Z");
equal(d.getEncodedHex(),
      "181332303135303133313030303030302e3132335a",
      "new GeneralizedTime(20150131000000.123Z)");

// -- JSON constructor test --
d = new _DERGeneralizedTime({
    'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))
});
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "new GeneralizedTime({date: new Date(?2015/01/31 00:00:00?)})");

d = new _DERGeneralizedTime({
    'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 120)),
    millis: true
});
equal(d.getEncodedHex(),
      "181232303135303133313030303030302e31325a",
      "new GeneralizedTime({date: new Date(?2015/01/31 00:00:00.12?)})");

d = new _DERGeneralizedTime({str: "20150131000000Z"});
equal(d.getEncodedHex(),
      "180f32303135303133313030303030305a",
      "new DERGeneralizedTime({str: 20150131000000Z})");

d = new _DERGeneralizedTime({str: "20150131000000.123Z"});
equal(d.getEncodedHex(),
      "181332303135303133313030303030302e3132335a",
      "new DERGeneralizedTime({str: 20150131000000.123Z})");

d = new _DERGeneralizedTime();
var d5hex = d.getEncodedHex()
equal(d5hex.substr(0, 4), "180f", "new GeneralizedTime() -> 180f...");
equal(d5hex.length, 34, "new GeneralizedTime().getEncodedHex().length()=34");

// now + millis test
// - fraction of second may not include with one in a hundred chance
// - so any of three times is fine, it will be fine.
var d6flag = false;
var s1 = "";
for (var i = 0; i < 3; i++) {
    var startMsec = new Date();
    while (new Date() - startMsec < 258);
    d = new _DERGeneralizedTime({millis: true});
    var d6hex = d.getEncodedHex();
    if (d6hex.substr(0, 2) != "18") continue;
    var hS = ASN1HEX.getV(d6hex, 0);
    var s = hextoutf8(hS);
    s1 += s + "/";
    if (s.indexOf(".") == -1) continue;
    d6flag = true;
}
equal(d6flag, true, "new GeneralizedTime({millis: true}): " + s1);
});

test("Sequence Test", function() {
  expect(4);
  var d1a = new KJUR.asn1.DERUTF8String();
  d1a.setString('a');
  var d1b = new KJUR.asn1.DERUTF8String();
  d1b.setString('b');
  var d1 = new KJUR.asn1.DERSequence();
  d1.setByASN1ObjectArray([d1a, d1b]);
  equal(d1.getEncodedHex(), "30060c01610c0162", "setByASN1ObjectArray");

  var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d2b = new KJUR.asn1.DERUTF8String({'str':'b'});
  var d2 = new KJUR.asn1.DERSequence({'array': [d2a, d2b]});
  equal(d2.getEncodedHex(), "30060c01610c0162", "constructor array");

  var d3 = new KJUR.asn1.DERSequence({'array': []});
  equal(d3.getEncodedHex(), "3000", "constructor array []");

  var d4 = new KJUR.asn1.DERSequence();
  equal(d4.getEncodedHex(), "3000", "constructor array no initialize");
});

test("Set Test", function() {
  expect(2);
  var d1a = new KJUR.asn1.DERUTF8String();
  d1a.setString('a');
  var d1b = new KJUR.asn1.DERUTF8String();
  d1b.setString('b');
  var d1 = new KJUR.asn1.DERSet();
  d1.setByASN1ObjectArray([d1a, d1b]);
  equal(d1.getEncodedHex(), "31060c01610c0162", "setByASN1ObjectArray");

  var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
  var d2b = new KJUR.asn1.DERUTF8String({'str':'b'});
  var d2 = new KJUR.asn1.DERSet({'array': [d2a, d2b]});
  equal(d2.getEncodedHex(), "31060c01610c0162", "constructor array");
});

// ----- TaggedObject ------------------------------

test("TaggedObject Test", function() {
var _DERTaggedObject = KJUR.asn1.DERTaggedObject;
var _DERInteger = KJUR.asn1.DERInteger;

var d;
var d1a = new KJUR.asn1.DERUTF8String();
d1a.setString('a'); // 0c0161
var d1 = new KJUR.asn1.DERTaggedObject();
d1.setASN1Object(true, "a0", d1a);
equal(d1.getEncodedHex(), "a0030c0161", "setASN1Object");

var d2a = new KJUR.asn1.DERUTF8String({'str':'a'});
var d2 = new KJUR.asn1.DERTaggedObject({'explicit':true,'tag':'a1','obj':d2a});
equal(d2.getEncodedHex(), "a1030c0161", "constructor with params");

var d3a = new KJUR.asn1.DERUTF8String({'str':'a'});
var d3 = new KJUR.asn1.DERTaggedObject({'explicit':false,'tag':'a1','obj':d2a});
equal(d3.getEncodedHex(), "a10161", "constructor with params implicit");

var d4a = new KJUR.asn1.DERUTF8String({'str':'a'});
var d4 = new KJUR.asn1.DERTaggedObject({'obj':d2a});
equal(d4.getEncodedHex(), "a0030c0161", "constructor with defaults");

d = new _DERTaggedObject({
  tage: "a2",
  obj: new _DERInteger({int: 7})
});
equal(d.getEncodedHex(), "a203020107", "tage a2 DERInteger(7)");

d = new _DERTaggedObject({
  tage: "a2",
  obj: {'int': {'int': 7}}
});
equal(d.getEncodedHex(), "a203020107", "tage a2 {int: {int: 7}}");

d = new _DERTaggedObject({
  tagi: "82",
  obj: {'int': {'int': 7}}
});
equal(d.getEncodedHex(), "820107", "tagi 82 {int: {int: 7}}");

d = new _DERTaggedObject({
  tage: "a2",
  obj: {'asn1': {'tlv': '020107'}}
});
equal(d.getEncodedHex(), "a203020107", "tage a2 {asn1: {tlv: ''020107'}}");

d = new _DERTaggedObject({
  tag: "81",
  str: "aaa"
});
equal(d.getEncodedHex(), "8103616161", "tag 81, str: aaa");

d = new _DERTaggedObject({
  tag: "81",
  hex: "616161"
});
equal(d.getEncodedHex(), "8103616161", "tag 81, hex: 616161");

});

// ----- ASN1Util.getPEMStringFromHex --------------

test("ASN1Util.getPEMStringFromHex", function() {
var s = "" +
"-----BEGIN HOGE HOGE-----\r\n" +
"YWFh\r\n" +
"-----END HOGE HOGE-----\r\n";

equal(KJUR.asn1.ASN1Util.getPEMStringFromHex("616161", "HOGE HOGE"), s, "aaa");
equal(KJUR.asn1.ASN1Util.getPEMStringFromHex(hCer1, "CERTIFICATE"), sCer1PEM, "z2 cer1 pem");
});

});
-->
</script>
  
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
<a href="index.html">TEST INDEX</a> | 
<a href="../api/index.html">API</a> | 
<a href="qunit-do-asn1-newobj.html">asn1-newobj</a> |
<a href="qunit-do-x509-hex2dn.html">x509-hex2dn</a> |
<a href="qunit-do-asn1x509.html">asn1x509</a> |
<a href="qunit-do-asn1x509-multirdn.html">asn1x509-multirdn</a> |
</body>
</html>
